home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / book / Chap03 / GLRect / GLRect.c next >
Encoding:
C/C++ Source or Header  |  1999-09-23  |  1.5 KB  |  75 lines

  1. // GLRect.c
  2. // The Drawing a simple 3D rectangle program with GLUT
  3. // OpenGL SuperBible, 2nd Edition
  4. // Richard S. Wright Jr.
  5.  
  6. #include <windows.h>
  7. #include <gl/glut.h>
  8.  
  9. // Called to draw scene
  10. void RenderScene(void)
  11.     {
  12.     // Clear the window with current clearing color
  13.     glClear(GL_COLOR_BUFFER_BIT);
  14.  
  15.        // Set current drawing color to red
  16.     //           R     G       B
  17.     glColor3f(1.0f, 0.0f, 0.0f);
  18.  
  19.     // Draw a filled rectangle with current color
  20.     glRectf(100.0f, 150.0f, 150.0f, 100.0f);
  21.  
  22.     // Flush drawing commands
  23.     glFlush();
  24.     }
  25.  
  26.  
  27. // Setup the rendering state
  28. void SetupRC(void)
  29.     {
  30.     // Set clear color to blue
  31.     glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
  32.  
  33.  
  34.     }
  35.  
  36.  
  37.  
  38. // Called by GLUT library when the window has chanaged size
  39. void ChangeSize(GLsizei w, GLsizei h)
  40.     {
  41.     // Prevent a divide by zero
  42.     if(h == 0)
  43.         h = 1;
  44.         
  45.     // Set Viewport to window dimensions
  46.     glViewport(0, 0, w, h);
  47.  
  48.     // Reset coordinate system
  49.     glMatrixMode(GL_PROJECTION);
  50.     glLoadIdentity();
  51.  
  52.     // Establish clipping volume (left, right, bottom, top, near, far)
  53.     if (w <= h) 
  54.         glOrtho (0.0f, 250.0f, 0.0f, 250.0f*h/w, 1.0, -1.0);
  55.     else 
  56.         glOrtho (0.0f, 250.0f*w/h, 0.0f, 250.0f, 1.0, -1.0);
  57.  
  58.     glMatrixMode(GL_MODELVIEW);
  59.     glLoadIdentity();
  60.     }
  61.  
  62. // Main program entry point
  63. void main(void)
  64.     {
  65.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  66.     glutCreateWindow("GLRect");
  67.     glutDisplayFunc(RenderScene);
  68.     glutReshapeFunc(ChangeSize);
  69.  
  70.     SetupRC();
  71.  
  72.     glutMainLoop();
  73.     }
  74.  
  75.